package net.xdclass.strategy;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

/**
 * @author Allen
 * @version 1.0
 * @date 2022/11/10 16:06
 */
public class CustomTablePreciseShardingAlgorithm implements PreciseShardingAlgorithm<String> {
	/**
	 * @param availableTargetNames 数据源集合
	 *                             在分库时值为所有分⽚库的集合 databaseNames
	 *                             分表时为对应分⽚库中所有分⽚表的集合 tablesNames
	 * @param shardingValue        分⽚属性，包括
	 *                             logicTableName 为逻辑表，
	 *                             columnName 分⽚健字段），
	 *                             value 为从 SQL 中解析出的分⽚健的值
	 * @return
	 */
	@Override
	public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> shardingValue) {
		String shardingValueValue = shardingValue.getValue();
		String tablePrefix = shardingValueValue.substring(shardingValueValue.length() - 1);

		String tableName = availableTargetNames.iterator().next();
		return tableName + "_" + tablePrefix;


	}
}
